package hu.lacimol.tutorial.todo.action;

import hu.lacimol.tutorial.todo.scheduling.TodoMonitor;
import hu.lacimol.tutorial.todo.util.GlobalSettings;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.atlassian.jira.web.action.JiraWebActionSupport;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;

public class SchedulerAction extends JiraWebActionSupport {
	
	private static final long serialVersionUID = -1993489970401317470L;
	
	private final TodoMonitor todoMonitor;
	private long interval;
	
	public SchedulerAction(TodoMonitor todoMonitor, PluginSettingsFactory pluginSettingsFactory) {
		this.todoMonitor = todoMonitor;
		this.interval = todoMonitor.getInterval();
	}
	
	@Override
	protected String doExecute() throws Exception {
		return SUCCESS;
	}
	
	public String doReschedule() {
		this.todoMonitor.reschedule(this.interval);
		return getRedirect("TodoScheduler.jspa");
	}
	
	public long getInterval() {
		return this.interval;
	}
	
	public void setInterval(long interval) {
		this.interval = interval;
	}
	
	public Date getLastRun() {
		return this.todoMonitor.getLastRun();
	}
	
	public Date getNextRun() {
		return this.todoMonitor.getNextRun();
	}
	
	public int getTodoSize() {
		return this.todoMonitor.getTodoSize();
	}
	
	public SimpleDateFormat getHunDateFormatter() {
		return GlobalSettings.createHunDateFormatter();
	}
	
}